set(CROSSROAD_STAGE_DIR ${CMAKE_CURRENT_BINARY_DIR}/stage CACHE STRING "Dependencies stage installation directory")

message(STATUS "Stage directory: ${CROSSROAD_STAGE_DIR}")

include(ExternalProject)
if(MSVC)
    set(DEP_BUILD_COMMAND BUILD_COMMAND ${CMAKE_COMMAND} --build . --config $<CONFIG> INSTALL_COMMAND ${CMAKE_COMMAND} --build . --config $<CONFIG> --target INSTALL)
endif()

set(ZLIB_TAG v1.2.11)
ExternalProject_Add(zlib
    GIT_REPOSITORY https://github.com/madler/zlib
    GIT_TAG ${ZLIB_TAG}
    GIT_SHALLOW 1
    UPDATE_COMMAND ""
    PATCH_COMMAND git reset --hard ${ZLIB_TAG}
          COMMAND git am ${CMAKE_CURRENT_SOURCE_DIR}/patches/zlib/0001-Do-not-use-debug-postfix.patch
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DBUILD_SHARED_LIBS=ON -DCMAKE_DEBUG_POSTFIX=
    ${DEP_BUILD_COMMAND})

if(UNIX)
    set(ZLIB_LIB "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}z${CMAKE_STATIC_LIBRARY_SUFFIX}")
else()
    set(ZLIB_LIB "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

set(LIBEVENT_TAG release-2.1.8-stable)
ExternalProject_Add(libevent
    GIT_REPOSITORY https://github.com/libevent/libevent
    GIT_TAG ${LIBEVENT_TAG}
    GIT_SHALLOW 1
    UPDATE_COMMAND ""
    PATCH_COMMAND git reset --hard ${LIBEVENT_TAG}
          COMMAND git am ${CMAKE_CURRENT_SOURCE_DIR}/patches/libevent/0001-win32-symbol-check.patch
          COMMAND git am ${CMAKE_CURRENT_SOURCE_DIR}/patches/libevent/0002-Fix-race-in-access-to-ev_res-from-event-loop-with-ev.patch
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_BENCHMARK=ON -DEVENT__DISABLE_TESTS=ON
        -DEVENT__DISABLE_REGRESS=ON -DEVENT__DISABLE_SAMPLES=ON
    ${DEP_BUILD_COMMAND})

set(LIBSODIUM_TAG 1.0.13)
ExternalProject_Add(libsodium
    GIT_REPOSITORY https://github.com/jedisct1/libsodium
    GIT_TAG ${LIBSODIUM_TAG}
    GIT_SHALLOW 1
    UPDATE_COMMAND ""
    PATCH_COMMAND git reset --hard ${LIBSODIUM_TAG}
          COMMAND git am ${CMAKE_CURRENT_SOURCE_DIR}/patches/libsodium/0001-added-cmake-project-files.patch
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    ${DEP_BUILD_COMMAND})

set(NGHTTP2_TAG v1.24.0)
ExternalProject_Add(nghttp2
    GIT_REPOSITORY https://github.com/nghttp2/nghttp2
    GIT_TAG ${NGHTTP2_TAG}
    GIT_SHALLOW 1
    UPDATE_COMMAND ""
    PATCH_COMMAND git reset --hard ${NGHTTP2_TAG}
          COMMAND git am ${CMAKE_CURRENT_SOURCE_DIR}/patches/nghttp2/0001-build-only-static-library.patch
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_INSTALL_LIBDIR=lib
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DENABLE_LIB_ONLY=ON
    ${DEP_BUILD_COMMAND})

if(WIN32)
    set(LIBCURL_EXTRA_ARGS -DCMAKE_USE_WINSSL=ON)
else()
    ExternalProject_Add(mbedtls
        URL https://github.com/ARMmbed/mbedtls/archive/mbedtls-2.5.1.tar.gz
        URL_HASH SHA256=63010b5d024aee4e6f708dfafe9c45c904dbbfab8b37bfe3ffbf46ac3405e3fa
        CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
            -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF)
    set(LIBCURL_EXTRA_DEPENDS mbedtls)
    set(LIBCURL_EXTRA_ARGS -DCMAKE_USE_OPENSSL=OFF -DCMAKE_USE_MBEDTLS=ON "-DMBEDTLS_INCLUDE_DIRS=${CROSSROAD_STAGE_DIR}/include"
        "-DMBEDTLS_LIBRARY=${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedtls${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "-DMBEDX509_LIBRARY=${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedx509${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "-DMBEDCRYPTO_LIBRARY=${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
    set(LIBCURL_EXTRA_LIBS
        "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedtls${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedx509${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

ExternalProject_Add(c-ares
    URL https://github.com/c-ares/c-ares/archive/cares-1_13_0.tar.gz
    URL_HASH SHA256=7c48c57706a38691041920e705d2a04426ad9c68d40edd600685323f214b2d57
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCARES_SHARED=OFF -DCARES_STATIC=ON -DCARES_STATIC_PIC=ON
    ${DEP_BUILD_COMMAND})
ExternalProject_Add(libcurl
    DEPENDS ${LIBCURL_EXTRA_DEPENDS} c-ares nghttp2 zlib
    URL https://curl.haxx.se/download/curl-7.55.1.tar.xz
    URL_HASH SHA256=3eafca6e84ecb4af5f35795dee84e643d5428287e88c041122bb8dac18676bb7
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CROSSROAD_STAGE_DIR}" -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DBUILD_CURL_EXE=OFF -DCURL_STATICLIB=ON -DUSE_NGHTTP2=ON -DCMAKE_USE_LIBSSH2=OFF
        -DENABLE_ARES=ON -DHTTP_ONLY=ON -DBUILD_TESTING=OFF -DCMAKE_DEBUG_POSTFIX=
        ${LIBCURL_EXTRA_ARGS}
        "-DCARES_INCLUDE_DIR=${CROSSROAD_STAGE_DIR}/include"
        "-DCARES_LIBRARY=${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}cares${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "-DNGHTTP2_INCLUDE_DIR=${CROSSROAD_STAGE_DIR}/include"
        "-DNGHTTP2_LIBRARY=${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}nghttp2${CMAKE_STATIC_LIBRARY_SUFFIX}"
        "-DZLIB_INCLUDE_DIR=${CROSSROAD_STAGE_DIR}/include" "-DZLIB_LIBRARY=${ZLIB_LIB}"
    ${DEP_BUILD_COMMAND})

set(CROSSROAD_DEP_LIBS "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}event${CMAKE_STATIC_LIBRARY_SUFFIX}"
    "${CROSSROAD_STAGE_DIR}/lib/libcurl${CMAKE_STATIC_LIBRARY_SUFFIX}"
    "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}nghttp2${CMAKE_STATIC_LIBRARY_SUFFIX}"
    "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}sodium${CMAKE_STATIC_LIBRARY_SUFFIX}"
    "${LIBCURL_EXTRA_LIBS}"
    "${CROSSROAD_STAGE_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}cares${CMAKE_STATIC_LIBRARY_SUFFIX}"
    "${ZLIB_LIB}"
    )
